<?php
namespace app\api_wp_v01\controller;

class StaffRecycleRegion extends Staff {
	
	public function get_grade() {
		
		$parameter = \think\Request::instance()->post();
		$judge_1 = array_key_exists('grade', $parameter) && is_integer($parameter['grade']) && in_array($parameter['grade'], array(1, 2, 3));
		$judge_2 = array_key_exists('parent_id', $parameter) && is_integer($parameter['parent_id']) && (0 <= $parameter['parent_id']);
		if(!$judge_1 || !$judge_2){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		$field = array(
			'`region`.`id`', '`region`.`name`',
		);
		$region_select = \app\common\model\RegionModel::field($field)->where('grade', $parameter['grade'])->where('parent_id', $parameter['parent_id'])->select();
		
		$region_list = array();
		foreach($region_select as $RegionModel){
			$region_list[] = array(
				'id' => $RegionModel->id,
				'name' => $RegionModel->name,
			);
		};
		
		return $this->disposeJson(0, '', array(
			'region_list' => $region_list,
		));
		
	}
	
	public function get_info() {
		
		$field = array(
			'`region`.*'
		);
		$region_select = \app\common\model\RegionModel::field($field)
			->join('staff_region', '`staff_region`.`region_id` = `region`.`id`', 'INNER')
			->where('staff_region.staff_id', $this->staff_id)
			->select();
		
		$region_list = array();
		foreach($region_select as $RegionModel){
			$region_list[] = array(
				'id' => $RegionModel->id,
				'name' => $RegionModel->name_full,
			);
		};
		
		$judge = \app\common\model\StaffRegionApplyModel::where('staff_id', $this->staff_id)->where('result', 0)->find();
		
		return $this->disposeJson(0, '', array(
			'region_list' => $region_list,
			'can_apply' => (null === $judge) ? true : false,
		));
	}
	
	public function apply() {
		
		$parameter = \think\Request::instance()->post();
		$judge_1 = array_key_exists('region_id', $parameter) && is_integer($parameter['region_id']) && (0 < $parameter['region_id']);
		if(!$judge_1){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		$RegionModel = \app\common\model\RegionModel::get($parameter['region_id']);
		if(null === $RegionModel){
			return $this->disposeJson(1, '目标区域不存在');
		}else if(3 !== $RegionModel->grade){
			return $this->disposeJson(1, '目标区域类型不正确');
		};
		
		$judge = \app\common\model\StaffRegionApplyModel::where('staff_id', $this->staff_id)->where('result', 0)->find();
		if(null !== $judge){
			return $this->disposeJson(1, '您当前存在未被处理的申请');
		};
		
		$judge = \app\common\model\StaffRegionModel::where('staff_id', $this->staff_id)->where('region_id', $parameter['region_id'])->find();
		if(null !== $judge){
			return $this->disposeJson(1, '您已经绑定了该地区，不能重复申请');
		};
		
		$time = time();
		$StaffRegionApplyModel = new \app\common\model\StaffRegionApplyModel();
		$StaffRegionApplyModel->staff_id = $this->staff_id;
		$StaffRegionApplyModel->region_id = $RegionModel->id;
		$StaffRegionApplyModel->create_time = $time;
		$result = $StaffRegionApplyModel->save();
		if(false == $result){
			return $this->disposeJson(1, '提交申请失败');
		};
		
		return $this->disposeJson(0, '');
		
	}
	
	public function remove() {
		
		$parameter = \think\Request::instance()->post();
		$judge_1 = array_key_exists('region_id', $parameter) && is_integer($parameter['region_id']) && (0 < $parameter['region_id']);
		if(!$judge_1){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		$result = \app\common\model\StaffRegionModel::where('staff_id', $this->staff_id)->where('region_id', $parameter['region_id'])->delete();
		if(1 !== $result){
			return $this->disposeJson(1, '操作失败，请刷新后重试');
		};
		
		return $this->disposeJson(0, '');
		
	}
	
}